package com.kaka.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaka.pojo.Order;
import com.kaka.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                          @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Order> orders = ordersService.findAll();
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        System.out.println(">>>>>>>>>>>>>>>>>>>>" + pageInfo);
        request.setAttribute("pageInfo", pageInfo);
        return "/orders/orders-list.jsp";
    }

    @RequestMapping("/findById")
    public String findById(Integer id, HttpServletRequest request) {
        Order order = ordersService.selectById(id);
        request.setAttribute("order", order);
        return "/orders/orders-show.jsp";
    }
}
